package com.ipad.net;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2018/12/3.
 */

public class ApiClient {
    /**
     * *网络配置
     */
    //    String BASE_URL = "https://api.heyimed.com";
    String IMAG_HTTP = "https://img01.heyimed.com/";
    //    String BASE_URL = "http://192.168.0.119";
    String BASE_URL = "http://39.98.238.183:8080";
//    String BASE_URL = "http://60.21.216.201:8090/";
//    String BASE_URL = "http://10.1.50.127:8080/";//运城
//    String BASE_URL = "http://123.177.19.13:2816";
    //        String BASE_URL = "http://172.24.255.103:8090";
    String UPDATE_URL = "https://api.heyimed.com/?";
    //正式服
//    String BASE_URL = "http://192.168.1.142:8080";
    public static String client_type = "android";
    public static String app_key = "225f220eeb22f8b1";
    public static String v = "1";
    public static String uid = "1";
    public static String app_secret = "846335d9d06f20b4f8e6489066fd5c2e";


    public static ApiClient apiClient = null;
    private Retrofit jRetorfit = null;
    private Retrofit pRetorfit = null;
    private static final long CONNECT_TIMEOUT = 15;
    private static final long READ_TIMEOUT = 15;
    private static final long WRITE_TIMEOUT = 15;


    public Retrofit getjRetorfit() {
        if (jRetorfit == null) {
            int cacheSize = 10 * 1024 * 1024;     // 10 MiB
            final String CACHE_PATH
                    = Environment.getExternalStorageDirectory().getAbsolutePath()
                    + "/okcache";
            File cacheDirectory = new File(CACHE_PATH);
            Cache cache = new Cache(cacheDirectory, cacheSize);


            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .cache(cache)
                    .addInterceptor(new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request.Builder builder2 = chain.request().newBuilder();
                            builder2.cacheControl(new CacheControl.Builder().maxStale(60 * 60 * 2, TimeUnit.SECONDS).build());
                            Response proceed = chain.proceed(builder2.build());

                            return proceed;
                        }
                    })
                    .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
                    .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
                    .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                    .addInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                        @Override
                        public void log(String message) {
                            Log.e("jRetorfit", "log: " + message);
                        }
                    }).setLevel(HttpLoggingInterceptor.Level.BODY))
                    .build();
            jRetorfit = new Retrofit.Builder()
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(okHttpClient)
                    .baseUrl(BASE_URL)
                    .build();
        }

        return jRetorfit;
    }

    public Retrofit getpRetorfit() {
        if (pRetorfit == null) {
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
                    .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
                    .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                    .addInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                        @Override
                        public void log(String message) {
                            Log.e("jRetorfit", "log: " + message);
                        }
                    }).setLevel(HttpLoggingInterceptor.Level.BODY))
                    .addInterceptor(new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request original = chain.request();
                            HttpUrl url=original.url().newBuilder()
                                    .addQueryParameter("client_type",ApiClient.client_type)
                                    .addQueryParameter("app_key",ApiClient.app_key)
                                    .addQueryParameter("v",ApiClient.v)
                                    .build();
                            //添加请求头
                            Request request = original.newBuilder()
                                    .url(url)
                                    .build();
                            return chain.proceed(request);
                        }
                    })
                    .build();
            pRetorfit = new Retrofit.Builder()
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(okHttpClient)
                    .baseUrl(UPDATE_URL)
                    .build();
        }

        return pRetorfit;
    }

    public static ApiClient getInstance() {
        if (apiClient == null) {
            apiClient = new ApiClient();
        }
        return apiClient;
    }

}
